/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery.noConflict();
var username;
var password;

var idReg;
var uname;
var pass;
var phone;
var email;
var fname;
var address;
var compound = /^[0-9\s]+$/gi;
var act;

function hideDiv(){
	jQuery(".wrapper").css("display","none");
}
function setPositionForm(){
    jQuery(".wrapper").css({
        "margin-top": 100 + "px",
        "margin-left": 200 + "px",
        "z-index":500
    });
    jQuery("#backgroundPopup").css({
        "display":"block",
        "opacity":0.3
    });
    jQuery("#backgroundPopup").fadeIn("slow");
}

function createXmlHttpRequestObject() {
    var ro;
    var browser = navigator.appName;
    // Need to determine IE7 and not do this.
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else {
        ro = new XMLHttpRequest();
    }
    return ro;
}

function createResponseXML(textXML) {
    // code for IE
    if (window.ActiveXObject) {
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(textXML);
        return xmlDoc;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var xmlDoc = parser.parseFromString(textXML, "text/xml");
        return xmlDoc;
    }
}

function loginUser(username,password,action){
    act = action;
    var url = '/Alluring_Decors-war/LoginServlet?username=' +username + '&password=' + password;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function registerUser(regId,uname,pass,fname,address,email,phone,action){
    act = action;
    var url = '/Alluring_Decors-war/UserServlet?id='+ regId +'&userName=' +uname + '&password=' + pass +
                '&fullName=' + fname + '&address=' + address + '&email=' + email + '&phone=' + phone +
                '&action=' + action;
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function finishUpdate(){
    if(aeRequest.readyState==4){
        if(aeRequest.status == 200){
            var response = createResponseXML(aeRequest.responseText);
            var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
            if (result == "success") {
                if(act == "add"){
                    alert("Register successful!");
                }
                if(act == "logout"){
                    window.location.href="index.jsp";
                }
                if(act == "login"){
                    window.location.href="index.jsp";
                }
            }else{
                alert("login failed!");
            }
        }
    }
}

function logoutUser(action){
    act = action;
    var url = '/Alluring_Decors-war/LogoutServlet';
    //alert(url);
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    aeRequest.onreadystatechange = finishUpdate;
    aeRequest.send(null);
}

function randomString() {
	var chars = "0123456789";
	var string_length = 6;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
	var rnum = Math.floor(Math.random() * chars.length);
	randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
String.prototype.trim = function()
{
	var trimmed = this.replace(/^\s+|\s+$/g, '') ;
	return trimmed;
};
jQuery(document).ready(function() {
    hideDiv();
    jQuery("#btnLogin").click(function(){
		if(jQuery(".wrapper").is(":hidden")){
                        setPositionForm();
                        jQuery(".wrapper").css("position","absolute");
			jQuery(".wrapper").slideDown("slow");
		}else{
			jQuery(".wrapper").slideUp("slow");
		}
    });
    jQuery("#backgroundPopup").click(function(){
        jQuery(".wrapper").slideUp("slow");
        jQuery("#backgroundPopup").css("display","none");
    });

    jQuery("#submit").live('click',function(){
        username = jQuery("#username").val();
        password = jQuery("#pass").val();
        if(username != null && username.length>0 && password != null && password.length>0){
            loginUser(username,password,"login");
        }
        jQuery(".wrapper").slideUp("slow");
        jQuery("#backgroundPopup").css("display","none");
    });

    jQuery("#btnLogout").click(function(){
        logoutUser("logout");
    });

    jQuery("#reg").live('click',function(){
        idReg = randomString();
        uname = jQuery("#uname").val();
        pass = jQuery("#password").val();
        fname = jQuery("#fname").val();
        address = jQuery("#add").val();
        email = jQuery("#email").val();
        phone = jQuery("#phone").val();

        if(uname != null && uname.length>0 && pass != null && pass.length>0 && fname != null
                && fname.length>0 && address != null && address.length>0 && email != null && email.length>0
                && phone != null && String(phone).match(compound)){
                registerUser(idReg,uname,pass,fname,address,email,phone,"add");
        }
        jQuery(".wrapper").slideUp("slow");
        jQuery("#backgroundPopup").css("display","none");
    });
});


